/**
 * ...
 * @author Krzysztof Rozalski
 */

package hapi.sprite.blit;

import flash.display.Graphics;
import flash.geom.Point;
import hapi.sprite.sheet.BitmapFillSpriteSheet;
import hapi.sprite.display.SpriteSheetDisplay;

class BlitGraphicsSpriteSheetDisplay implements SpriteSheetDisplay {
	
	var spriteSheet:BitmapFillSpriteSheet;
	var dest:Point;
	public var visible(default, null):Bool;
	public var currentFrame(default, null):Int;
	public var totalFrames(default, null):Int;
	
	public function new(spriteSheet:BitmapFillSpriteSheet, ?x:Float=0.0, ?y:Float=0.0) {
		this.dest = new Point(x, y);
		this.spriteSheet = spriteSheet;
		this.totalFrames = spriteSheet.totalFrames;
		this.currentFrame = 1;
		this.visible = true;
	}
	
	public inline function isVisible():Bool {
		return visible;
	}
	
	public function show():Void {
		this.visible = true;
	}
	
	public function hide():Void {
		this.visible = false;
	}
	
	public function goto(frame:Int) {
		if(frame <= totalFrames && frame >= 1 && this.currentFrame != frame) {
			this.currentFrame = frame;
		}
	}
	
	public inline function renderOn(canvas:Graphics) {
		if(isVisible()) {
			spriteSheet.drawSpriteOn(canvas, currentFrame, dest);
		}
	}
	
	public inline function moveTo(x:Float, y:Float):Void {
		dest.x = x;
		dest.y = y;
	}
	
}